package list;

/**
 * description:
 * author:张腾
 * date:2021-03-29
 */

import java.util.Iterator;

/**
 * 单向链表API设计
 * @param <T>
 */
public class LinkList<T> implements Iterable{
    //记录头节点
    private Node head;
    //记录链表长度
    private int N;

    //节点类
    private class Node{
        //存储数据
        T item;
        //下一个节点
        Node next;

        public Node(T item,Node next){
            this.item = item;
            this.next = next;
        }
    }

    public LinkList(){
        this.head = new Node(null,null);
        this.N = 0;
    }

    //将线性表置为空表
    public void clear(){
        head.next = null;
        this.N = 0;
    }

    //判断当前线性表是否为空表
    public boolean isEmpty(){
        return this.N==0;
    }

    //获取线性表的长度
    public int length(){
        return N;
    }

    //获取指定位置的元素
    public T get(int i){
        Node n = head.next;
        for(int index = 0;index < i;index ++){
            n = n.next;
        }
        return n.item;
    }

    //向线性表添加元素t
    public void insert(T t){
        Node n = head;
        while (n.next!=null){
            n = n.next;
        }
        Node newNode = new Node(t,null);
        n.next = newNode;
        N++;
    }

    //向线性表指定i位置添加元素
    public void insert(int i,T t){
        Node pre = head;
        //找到i位置前一个结点
        for(int index = 0;index <= i-1;index ++){
            pre = pre.next;
        }
        Node curr = pre.next;
        Node newNode = new Node(t,curr);
        pre.next = newNode;
        N++;
    }

    //删除指定位置的元素，并返回该元素
    public T remove(int i){
        Node pre = head;
        //找到i位置前一个结点
        for(int index = 0;index <= i-1;index ++){
            pre = pre.next;
        }
        Node curr = pre.next;
        Node nextNode = curr.next;
        pre.next = nextNode;
        N--;
        return curr.item;
    }

    //查找t元素第一次出现的位置
    public int indexOf(T t){
        Node n = head;
        for (int i = 0;n.next!=null;i ++){
            n = n.next;
            if (n.item.equals(t)){
                return i;
            }
        }
        return -1;
    }

    @Override
    public Iterator iterator() {
        return new LIterator();
    }

    private class LIterator implements Iterator{
        private Node n;

        public LIterator(){
            this.n = head;
        }

        @Override
        public boolean hasNext() {
            return n.next!=null;
        }

        @Override
        public Object next() {
            n = n.next;
            return n.item;
        }
    }

    //反转整个链表
    public void reverse(){
        if (isEmpty()){
            return;
        }

        reverse(head.next);
    }

    public Node reverse(Node curr){
        if (curr.next==null){
            head.next = curr;
            return curr;
        }
        //递归反转当前节点的下一个节点
        Node pre = reverse(curr.next);

        pre.next = curr;
        curr.next = null;
        return curr;
    }

    //判断链表是否有环
    public boolean isCircle(Node first){

        Node fast = first;
        Node slow = first;

        while (first!=null && first.next!=null){
            first = first.next.next;
            slow = slow.next;

            if (first==slow){
                return true;
            }
        }

        return false;
    }

    /**
     * 若链表有环，求链表中环的入口
     * 当快慢指针相遇时，即链表有环，这时重新设定一个新指针指向链表的起点，且步长和慢指针一样为1，则慢指针和新指针相遇的地方就是环的入口
     * 证明该结论涉及到数论知识尚不讨论
     */

    public Node detectCycle(Node head) {

        Node fast = head, slow = head;
        while (true) {
            if (fast == null || fast.next == null) return null;
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) break;
        }
        fast = head;
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return fast;

    }
}
